<?php
/**
 * Created by JetBrains PhpStorm.
 * File: SectionAction.php
 * User: Xnfy520@gmail.com
 * Date: 12-3-13
 * Time: 下午5:25
 */

	class SectionAction extends CommonAction{

		//指向单元列表
		function index(){
			$section = D('Section');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}
					$list = $section->where($datas)->order('id desc')->select();
					$count = $section->where($datas)->count('id');
					if($count<1){
						//	$this->assign('isshow', 'no');
						$this->error('没有该单元数据');
					}elseif($count<C('PAGENUM')){
						$pagedata = array(1);
					}else{
						$pagedata = array(0,1,2,3,4,5,7,8);
					}
					$page = new page($count, C('PAGENUM'));

					$show = $page->fpage($pagedata);
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');

				}

			}else{
				$count = $section->count();
				if($count<C('PAGENUM')){
					$pagedata = array(1);
				}else{
					$pagedata = array(0,1,2,3,4,5,7,8);
				}
				$page = new page($count, C('PAGENUM'));
				$show = $page->fpage($pagedata);
				$list = $section->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
			//	dump($list);
				if($count==0){
					$this->assign('isshow', 'no');
					$this->assign('nobody', 'no');
				}else{
					$this->assign('isshow', 'yes');
					$this->assign('nobody', 'yes');
				}
			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();
		}

		//指向添加单元页面
		function add(){
			$this->display('add');
		}

		//添加单元
		function insert(){
			$role = D('Section');
			if($list = $role->create()){
				if($role->add()){
					$this->success('添加单元成功', __APP__.'/Section/index');
				}else{
					$this->error('添加单元失败');
				}
			}else{
				$this->error($role->getError());
			}
		}

		//删除单元
		function del(){
			if(isset($_POST['del']) && !empty($_POST['del'])){
				$section = D('Section');
				foreach($_POST['del'] as $key=>$value){
					$sectionname = $section->where('id='.$value)->getField('name');
					$list = $section->relation(true)->where('id='.$value)->select();
					if(count($list[0]['category'])>0){
						$this->error('[ '.$sectionname.' ]单元下还有分类, 请先清空');
					}else{
						$num = $section->relation(true)->delete($value);
					}
					if($num>0){
						$this->success('删除单元成功');
					}else{
						$this->error('删除单元失败');
					}
				}

			}else{
				$this->error('异常操作');
			}
		}

		//指向修改单元列表
		function edit(){
			if($_GET['id'] && !empty($_GET['id'])){
				$section = M('Section');
				$list = $section->find($_GET['id']);
				$this->assign('list', $list);
				$this->display();
			}else{
				$this->error('异常操作');
			}
		}

		//修改单元列表
		function update(){
			if(isset($_POST['id']) && !empty($_POST['id'])){
				$section = D('Section');
				if($section->create()){
					if($section->save()){
						$this->success('修改单元成功', __APP__.'/Section/index');
					}else{
						$this->error('修改单元失败');
					}
				}else{
					$this->error($section->getError());
				}
			}else{
				$this->error('异常操作');
			}
		}

		function select(){

			$section = D('Section');
			import("ORG.Util.page");

			if(isset($_POST['search'])){
				if(empty($_POST['key'])){
					$this->error('不能为空');
				}else{
					$datas = array();
					switch($_POST['search']){
						case 1 :
							$datas['id'] = $_POST['key'];
							break;
						case 2:
							$datas['name'] = array("like","%{$_POST['key']}%");
							break;
					}

					if(isset($_SESSION['mid']) && !empty($_SESSION['mid'])){
						if(isset($_SESSION['cid']) && !empty($_SESSION['cid'])){

							$list = $section->where($datas)->order('id desc')->select();
							$count = $section->where($datas)->count('id');
							if($count<1){
								//	$this->assign('isshow', 'no');
								$this->error('没有该单元数据');
							}elseif($count<C('PAGENUM')){
								$pagedata = array(1);
							}else{
								$pagedata = array(0,1,2,3,4,5,7,8);
							}
							$page = new page($count, C('PAGENUM'));

							$show = $page->fpage($pagedata);
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');

						}else{
							$this->error('异常操作');
						}
					}else{
						$this->error('异常操作');
					}


				}

			}else{

				if(isset($_GET['mid']) && !empty($_GET['mid'])){
					if(isset($_GET['cid']) && !empty($_GET['cid'])){
						$_SESSION['mid'] = $_GET['mid'];
						$_SESSION['cid'] = $_GET['cid'];
						$count = $section->count();
						if($count<C('PAGENUM')){
							$pagedata = array(1);
						}else{
							$pagedata = array(0,1,2,3,4,5,7,8);
						}
						$page = new page($count, C('PAGENUM'));
						$show = $page->fpage($pagedata);
						$list = $section->relation(true)->limit($page->getlimitfirst().','.C('PAGENUM'))->order('id desc')->select();
						//	dump($list);
						if($count==0){
							$this->assign('isshow', 'no');
							$this->assign('nobody', 'no');
						}else{
							$this->assign('isshow', 'yes');
							$this->assign('nobody', 'yes');
						}
					}else{
						$this->error('异常操作');
					}
				}else{
					$this->error('异常操作');
				}


			}

			$this->assign('show', $show);
			$this->assign('list', $list);
			$this->display();

		}

	}